27. Insert data in a database and select it

Insert Data in a Database and Select it


Quiz - Practice SQL INSERT Command

Hint: The SQL/SQLite Commands Cheat Sheet can be accessed here. It is also available from the Resources Tab.

Note #1: The gender attribute should be inserted as an integer value (e.g. Male = 1)

Note #2: Tommy is a Male Pomeranian that weighs 4 Kilograms.

Note #3: For Step 2, the .mode command provides several output options aside from column. For example we could have used, .mode tabs to have tab-separated values. Remember you can see all the output modes by checking .help in sqlite or review the section "Special commands to sqlite3 (dot-commands)" from this sqlite documentation.

QUESTION:

Note: The original format of the quiz has been modified.

Paste result you get after inserting Garfield in the pets table and changing the mode to ascii (given that Tommy is already in the row above).

SOLUTION:

NOTE: The solutions are expressed in RegEx pattern. Udacity uses these patterns to check the given answer

Solution

This is the result from selecting all pets and all columns from the database using ascii mode.

1TommyPomeranian142GarfieldTabby18


Explanation

Here are all the steps together to obtain the solution.

Let's break it down line-by-line:

Create the pets table

CREATE TABLE pets (_id INTEGER, name TEXT, breed TEXT, gender INTEGER, weight INTEGER);

Insert information about Tommy in row 1

INSERT INTO pets(_id, name, breed, gender, weight) VALUES(1, "Tommy", "Pomeranian", 1, 4);

Insert data about Garfield in row 2

INSERT INTO pets(_id, name, breed, gender, weight) VALUES(2, "Garfield", "Tabby", 1, 8);

Change the mode to ascii

```query
.mode ascii

###  Read all the columns and all the rows from the pets table

query
select * from pets;
```